package lc.q250_300;

import java.util.Iterator;

public class Q284 {

    public static class PeekingIterator implements Iterator<Integer> {

        private Iterator<Integer> iterator;

        private Integer first = null;

        public PeekingIterator(Iterator<Integer> iterator) {
            // initialize any member here.
            this.iterator = iterator;
            if (iterator.hasNext()) {
                first = iterator.next();
            }
        }

        public Integer peek() {
            // 由于迭代器向后迭代后前元素没了，那么这里可以考虑先迭代一个数，如果
            return first;
        }

        @Override
        public boolean hasNext() {
            return first != null;
        }

        @Override
        public Integer next() {
            Integer next = first;
            if (iterator.hasNext()) {
                first = iterator.next();
            } else {
                first = null;
            }
            return next;
        }
    }
}
